package com.hkfn.rfb.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.hkfn.rfb.common.utils.StringUtils;
import com.hkfn.rfb.common.utils.Utils;

/**
 * Created by zf-note on 2017/12/11.
 */
public class CircleProgressView extends View {

    private int mMaxProgress = 100;

    private int mProgress = 30;

    private int mCircleLineStrokeWidth = 2;

    private final int mTxtStrokeWidth = 2;

    // 画圆所在的距形区域
    private final RectF mRectF;

    private final Paint mPaint;

    private final Context mContext;

    private String mTxtHint1;

    private String mTxtHint2;

    private String progressColor = "#ff0000";

    public CircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCircleLineStrokeWidth = Utils.dip2px(context, mCircleLineStrokeWidth);
        mContext = context;
        mRectF = new RectF();
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();

        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }
        mPaint.setColor(0x00000000);

        mRectF.left = mCircleLineStrokeWidth; // 左上角x
        mRectF.top = mCircleLineStrokeWidth; // 左上角y
        mRectF.right = width - mCircleLineStrokeWidth; // 左下角x
        mRectF.bottom = height - mCircleLineStrokeWidth; // 右下角y
        canvas.drawArc(mRectF, -90, 360, true, mPaint);

        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.rgb(0xff, 0xff, 0xff));

        canvas.drawColor(Color.TRANSPARENT);
        mPaint.setStrokeWidth(mCircleLineStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        // 位置
        mRectF.left = mCircleLineStrokeWidth / 2; // 左上角x
        mRectF.top = mCircleLineStrokeWidth / 2; // 左上角y
        mRectF.right = width - mCircleLineStrokeWidth / 2; // 左下角x
        mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 右下角y

        // 绘制圆圈，进度条背景
        canvas.drawArc(mRectF, -90, 360, false, mPaint);
        mPaint.setColor(Color.parseColor(progressColor));
        canvas.drawArc(mRectF, -90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);

        mPaint.setColor(Color.WHITE);
        // 绘制进度文案显示
        mPaint.setStrokeWidth(mTxtStrokeWidth);
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);
    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.mMaxProgress = maxProgress;
    }

    public void setProgress(int progress) {
        this.mProgress = progress;
        this.invalidate();
    }

    public int getmProgress(){
        return mProgress;
    }

    public void setProgressNotInUiThread(int progress) {
        this.mProgress = progress;
        this.postInvalidate();
    }

    public String getmTxtHint1() {
        return mTxtHint1;
    }

    public void setmTxtHint1(String mTxtHint1) {
        this.mTxtHint1 = mTxtHint1;
    }

    public String getmTxtHint2() {
        return mTxtHint2;
    }

    public void setmTxtHint2(String mTxtHint2) {
        this.mTxtHint2 = mTxtHint2;
    }

    public void setProgressColor(String progressColor) {
        if (StringUtils.isEmpty(progressColor)) {
            return;
        }
        if (!progressColor.startsWith("#")) {
            progressColor = "#" + progressColor;
        }
        this.progressColor = progressColor;
    }
}